package co.yixiang.modules.shop.service.impl;

import cn.hutool.core.util.ObjectUtil;
import co.yixiang.exception.BadRequestException;
import co.yixiang.modules.shop.domain.YxStoreCategory;
import co.yixiang.modules.shop.domain.YxSupplier;
import co.yixiang.modules.shop.repository.YxStoreProductRepository;
import co.yixiang.modules.shop.repository.YxSupplierRepository;
import co.yixiang.modules.shop.service.YxSupplierService;
import co.yixiang.modules.shop.service.dto.YxStoreCategoryDTO;
import co.yixiang.modules.shop.service.dto.YxSupplierDto;
import co.yixiang.modules.shop.service.dto.YxSupplierQueryCriteria;
import co.yixiang.modules.shop.service.mapper.YxStoreProductMapper;
import co.yixiang.modules.shop.service.mapper.YxSupplierMapper;
import co.yixiang.utils.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;

/**
 * @author frq
 * @date 2020-06-11
 */
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class YxSupplierServiceImpl implements YxSupplierService {
    @Autowired
    private YxSupplierRepository yxSupplierRepository;
    @Autowired
    private YxSupplierMapper yxSupplierMapper;
    /**
     * 查询数据分页
     * @param criteria
     * @param pageable
     * @return
     */
    @Override
    public Map<String, Object> queryAll(YxSupplierQueryCriteria criteria, Pageable pageable) {
        Page<YxSupplier> page = yxSupplierRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(yxSupplierMapper::toDto));
    }
    /**
     * 查询所有数据不分页
     * @param criteria
     * @return
     */
    @Override
    public List<YxSupplierDto> queryAll(YxSupplierQueryCriteria criteria) {
        return yxSupplierMapper.toDto(yxSupplierRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));

    }
    /**
     * 根据ID查询
     * @param id
     * @return
     */
    @Override
    public YxSupplierDto findById(Integer id) {
        Optional<YxSupplier> yxSupplier = yxSupplierRepository.findById(id);
        ValidationUtil.isNull(yxSupplier,"YxSupplier","id",id);
        return yxSupplierMapper.toDto(yxSupplier.get());
    }
    /**
     * 创建
     * @param resources
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public YxSupplierDto create(YxSupplier resources) {
        resources.setAdder(SecurityUtils.getUsername());
        resources.setAddTime(new Date());
        return yxSupplierMapper.toDto(yxSupplierRepository.save(resources));
    }
    /**
     * 编辑
     * @param resources
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(YxSupplier resources) {
        Optional<YxSupplier> optional = yxSupplierRepository.findById(resources.getId());
        ValidationUtil.isNull( optional,"YxSupplier","id",resources.getId());
        YxSupplier yxSupplier = optional.get();
        yxSupplier.copy(resources);
        yxSupplierRepository.save(yxSupplier);
    }
    /**
     * 删除
     * @param ids
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(Integer[] ids) {
        for (Integer id:ids) {
            yxSupplierRepository.deleteById(id);
        }
    }
    /**
     * 根据供应商编码查询供应商
     * @param code
     * @return
     */
    @Override
    public YxSupplier findByCode(String code) {
        return yxSupplierRepository.findByCode(code);
    }
}
